package com.qyhl.module_practice.center.level;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.module_practice.R;
import com.qyhl.module_practice.R2;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.DateUtils;
import com.qyhl.webtv.basiclib.utils.NetUtil;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeLevelBean;
import com.qyhl.webtv.commonlib.utils.action.ActionConstant;
import com.qyhl.webtv.commonlib.utils.action.ActionLogUtils;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route( path = ARouterPathConstant.PRACTICE_LEVEL)
public class PracticeLevelActivity extends BaseActivity implements PracticeLevelContract.PracticeLevelView {
    @BindView(R2.id.title)
    TextView title;
    @BindView(R2.id.progress_txt)
    TextView progressTxt;
    @BindView(R2.id.progress_bar)
    ProgressBar progressBar;
    @BindView(R2.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;

    private List<PracticeLevelBean> mDataList = new ArrayList<>();

    private CommonAdapter<PracticeLevelBean> commonAdapter;

    private PracticeLevelPresenter mPresenter;

    private int level;

    private int duration;

    @Override
    protected int getContentView() {
        return R.layout.practice_activity_level;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        mImmersionBar.fullScreen(true).statusBarDarkFont(false).init();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void init() {
        loadMask.setStatus(LoadingLayout.Loading);
        mPresenter = new PracticeLevelPresenter(this);
        duration = getIntent().getIntExtra("duration", 0);
        level = getIntent().getIntExtra("level", 0);

        switch (level) {
            case 0:
                title.setText("暂无星级");
                break;
            case 1:
                title.setText("一星志愿者");
                break;
            case 2:
                title.setText("二星志愿者");
                break;
            case 3:
                title.setText("三星志愿者");
                break;
            case 4:
                title.setText("四星志愿者");
                break;
            default:
                title.setText("五星志愿者");
                break;
        }


        recycleView.setLayoutManager(new LinearLayoutManager(this));

        recycleView.setAdapter(commonAdapter = new CommonAdapter<PracticeLevelBean>(this, R.layout.practice_item_level, mDataList) {
            @Override
            protected void convert(ViewHolder holder, PracticeLevelBean bean, int position) {
                holder.setText(R.id.title, "志愿服务总时长≥" + bean.getLowerLimit() + "小时");
                switch (position) {
                    case 0:
                        holder.setVisible(R.id.divider, true);
                        if (bean.getLevel() <= level) {
                            holder.setImageResource(R.id.medal, R.drawable.practice_level_one_on_icon);
                        } else {
                            holder.setImageResource(R.id.medal, R.drawable.practice_level_one_off_icon);
                        }
                        break;
                    case 1:
                        holder.setVisible(R.id.divider, true);
                        if (bean.getLevel() <= level) {
                            holder.setImageResource(R.id.medal, R.drawable.practice_level_two_on_icon);
                        } else {
                            holder.setImageResource(R.id.medal, R.drawable.practice_level_two_off_icon);
                        }
                        break;
                    case 2:
                        holder.setVisible(R.id.divider, true);
                        if (bean.getLevel() <= level) {
                            holder.setImageResource(R.id.medal, R.drawable.practice_level_three_on_icon);
                        } else {
                            holder.setImageResource(R.id.medal, R.drawable.practice_level_three_off_icon);
                        }
                        break;
                    case 3:
                        holder.setVisible(R.id.divider, true);
                        if (bean.getLevel() <= level) {
                            holder.setImageResource(R.id.medal, R.drawable.practice_level_four_on_icon);
                        } else {
                            holder.setImageResource(R.id.medal, R.drawable.practice_level_four_off_icon);
                        }
                        break;
                    case 4:
                        holder.setVisible(R.id.divider, false);
                        if (bean.getLevel() <= level) {
                            holder.setImageResource(R.id.medal, R.drawable.practice_level_five_on_icon);
                        } else {
                            holder.setImageResource(R.id.medal, R.drawable.practice_level_five_off_icon);
                        }
                        break;
                }
            }
        });

        mPresenter.getLevel();
    }

    @Override
    protected void setListener() {
        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadMask.setReloadButtonText("加载中...");
                mPresenter.getLevel();
            }
        });
    }

    @OnClick(R2.id.back_btn)
    public void onViewClicked() {
        finish();
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void setLevel(List<PracticeLevelBean> beanList) {
        loadMask.setStatus(LoadingLayout.Success);
        for (int i = 0; i < beanList.size(); i++) {
            if (beanList.get(i).getLevel() == level) {
                progressTxt.setText(DateUtils.PracticeServiceTimeFormat(duration) + "/" + beanList.get(i).getUpperLimit() + "小时");
                progressBar.setMax(beanList.get(i).getUpperLimit() * 2);
                progressBar.setProgress((int) Math.round((double) duration / 1800));
            }
        }

        mDataList.clear();
        mDataList.addAll(beanList);
        commonAdapter.notifyDataSetChanged();
    }

    @Override
    public void setError(String msg) {
        loadMask.setStatus(LoadingLayout.Error);
        loadMask.setReloadButtonText("点击重试~");
        if (!NetUtil.isNetworkAvalible(this)) {
            loadMask.setErrorImage(R.drawable.error_network);
            loadMask.setErrorText("网络异常，请检查您的网络！");
        } else if (msg.contains("暂无")) {
            loadMask.setErrorImage(R.drawable.empty_content);
            loadMask.setErrorText(msg);
        } else {
            loadMask.setErrorImage(R.drawable.error_content);
            loadMask.setErrorText(msg);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        ActionLogUtils.getInstance().onResume(this, ActionConstant.PRACTICE_VOL_LEVEL);
    }

    @Override
    protected void onPause() {
        super.onPause();
        ActionLogUtils.getInstance().onPause(this, ActionConstant.PRACTICE_VOL_LEVEL);
    }

}
